/**
 * @p.md
 * 
 * Future 类用于异步操作的结果处理
 */
import { PID } from './actor/pid';

/**
 * Future 类，用于处理异步操作的结果
 */
export class Future<T = any> {
  private _promise: Promise<T>;
  private _resolve!: (value: T) => void;
  private _reject!: (reason: any) => void;
  private _pid: PID;
  
  /**
   * 创建 Future
   */
  constructor() {
    this._promise = new Promise<T>((resolve, reject) => {
      this._resolve = resolve;
      this._reject = reject;
    });
    
    // 创建一个虚拟的 PID，用于接收响应
    this._pid = new PID('future', `future-${Date.now()}-${Math.floor(Math.random() * 1000000)}`);
  }
  
  /**
   * 获取 Promise
   */
  promise(): Promise<T> {
    return this._promise;
  }
  
  /**
   * 获取 PID
   */
  get pid(): PID {
    return this._pid;
  }
  
  /**
   * 解析 Future
   * @param value 结果值
   */
  resolve(value: T): void {
    this._resolve(value);
  }
  
  /**
   * 拒绝 Future
   * @param reason 拒绝原因
   */
  reject(reason: any): void {
    this._reject(reason);
  }
} 